package clientApp;

import java.util.ArrayList;

import clientApp.utils.RPG_Workstation_Container;

import game.referenceValues.Feat;
import game.referenceValues.UnitClass;
import game.units.Attribute;
import game.units.Character;

/**
 *
 * @author Gabriel
 */
@SuppressWarnings("serial")
public class CharacterBuilder5 extends javax.swing.JPanel {

	RPG_Workstation_Container container;
	Character character;
	UnitClass lvlClass;
	ArrayList<Feat> featsArray;
	ArrayList<Feat> charFeatsArray;
	boolean isNewChar = false;
	Feat tempFeat;
	int remainingFeats;
	
    /** Creates new form CharacterBuilder5 */
    public CharacterBuilder5(RPG_Workstation_Container rpg_Workstation_Container, Character character, UnitClass lvlClass, boolean isNewChar) {
    	this.container = rpg_Workstation_Container; 
    	this.character = character;
    	this.lvlClass = lvlClass;
    	this.featsArray = Feat.getDnDFeats();
    	this.charFeatsArray = character.getFeats();
    	for (Feat cha : charFeatsArray) {
			for (Feat feat : featsArray) {
				if(feat.getName().equals(cha.getName()))
				{
					featsArray.remove(feat);
					break;
				}
			}
		}
    	this.isNewChar = isNewChar;
    	initComponents();
    	refreshArrays();
    	remainingFeats = character.remainingFeats();
    	if(character.extraFeats > 0)
    	{
    		remainingFeats = remainingFeats + character.extraFeats;
    		character.extraFeats = 0;
    	}
    	jTFFeatsLeft.setText(""+remainingFeats);    	
    }


	private void refreshArrays() {
		
		jListYourFeats.setModel(new javax.swing.AbstractListModel() {
        	ArrayList<Feat> feats = charFeatsArray;
            public int getSize() { return feats.size(); }
            public Object getElementAt(int i) { return feats.get(i).getName(); }
        });

		jListFeats.setModel(new javax.swing.AbstractListModel() {
        	ArrayList<Feat> feats = featsArray;
            public int getSize() { return feats.size(); }
            public Object getElementAt(int i) { return feats.get(i).getName(); }
        });
		
	}

    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListFeats = new javax.swing.JList();
        jBSelect = new javax.swing.JButton();
        jBRemove = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListYourFeats = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jBNext = new javax.swing.JButton();
        jBBack = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jBViewFeat = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTAFeatDetails = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jTFFeatsLeft = new javax.swing.JTextField();

        jPanel1.setBackground(new java.awt.Color(0, 0, 102));
        jPanel1.setMinimumSize(new java.awt.Dimension(800, 600));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 28));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Character Feats");

        jScrollPane1.setViewportView(jListFeats);

        jBSelect.setText("Select");
        jBSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSelectActionPerformed(evt);
            }
        });

        jBRemove.setText("Remove");
        jBRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRemoveActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(jListYourFeats);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Your Feats:");

        jBNext.setText("Next");
        jBNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNextActionPerformed(evt);
            }
        });

        jBBack.setText("Back");
        jBBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBBackActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Feats List:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Feat Details:");

        jBViewFeat.setText("View Details");
        jBViewFeat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBViewFeatActionPerformed(evt);
            }
        });

        jTAFeatDetails.setColumns(20);
        jTAFeatDetails.setRows(5);
        jScrollPane3.setViewportView(jTAFeatDetails);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Remaining Feats:");

        jTFFeatsLeft.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 785, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(18, 18, 18)
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                                        .addComponent(jBSelect, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                                                        .addGap(51, 51, 51))
                                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                                        .addComponent(jBViewFeat)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                        .addComponent(jTFFeatsLeft, javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))))
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jBRemove))
                                    .addGap(18, 18, 18))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 435, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jBBack)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jBNext)))))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jBRemove, jBSelect});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jBNext)
                            .addComponent(jBBack)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jBRemove)
                                    .addComponent(jBSelect))
                                .addGap(18, 18, 18)
                                .addComponent(jBViewFeat)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTFFeatsLeft, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(27, 27, 27))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBSelectActionPerformed(java.awt.event.ActionEvent evt) {                                         
    	if(jListFeats.getSelectedIndex() >= 0 && remainingFeats > 0)
    	{
    		character.getFeats().add(featsArray.get(jListFeats.getSelectedIndex()));
    		tempFeat = featsArray.get(jListFeats.getSelectedIndex());
    		featsArray.remove(jListFeats.getSelectedIndex());
    		charFeatsArray = character.getFeats();
    		refreshArrays();
    		remainingFeats = remainingFeats -1;
    		jTFFeatsLeft.setText(""+remainingFeats); 
    	}      	
    }

    private void jBRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRemoveActionPerformed
    	if(jListYourFeats.getSelectedIndex() >= 0)
    	{
    		if(tempFeat.getName().equals(character.getFeats().get(jListYourFeats.getSelectedIndex()).getName())){    		
    			character.getFeats().remove(jListYourFeats.getSelectedIndex());
        		featsArray.add(tempFeat);
        		charFeatsArray = character.getFeats();
        		tempFeat = null;
        		refreshArrays();  
        		remainingFeats = remainingFeats +1;
        		jTFFeatsLeft.setText(""+remainingFeats); 
    		}
    		else{
				
			}
    		  		
    	}
    }//GEN-LAST:event_jBRemoveActionPerformed

    private void jBViewFeatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBViewFeatActionPerformed
    	if(jListFeats.getSelectedIndex() >= 0)
    	{
    		jTAFeatDetails.setText(featsArray.get(jListFeats.getSelectedIndex()).getDescription());
    	}  
    }//GEN-LAST:event_jBViewFeatActionPerformed
        
    private void jBNextActionPerformed(java.awt.event.ActionEvent evt) {                                       

    	character.setInitiative(character.getAttributes().get(Attribute.DEXTERITY)
				.getModifier() + character.bonusInitiative);
    	if(lvlClass.isCaster())
    	{
    		container.add(new CharacterBuilder6(container, character, lvlClass, isNewChar), "CharacterBuilder6");
            container.panels.show(container, "CharacterBuilder6");
    	}
    	else
    	{
    		container.add(new CharacterBuilder7(container, character, lvlClass, isNewChar), "CharacterBuilder7");
            container.panels.show(container, "CharacterBuilder7");
    	}
   	 	
    }

    private void jBBackActionPerformed(java.awt.event.ActionEvent evt) {                                       
    	// TODO salvar o char
   	 	container.add(new CharacterBuilder4(container, character, lvlClass, isNewChar), "CharacterBuilder4");
        container.panels.show(container, "CharacterBuilder4");
    }
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBBack;
    private javax.swing.JButton jBNext;
    private javax.swing.JButton jBRemove;
    private javax.swing.JButton jBSelect;
    private javax.swing.JButton jBViewFeat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jListFeats;
    private javax.swing.JList jListYourFeats;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea jTAFeatDetails;
    private javax.swing.JTextField jTFFeatsLeft;
    // End of variables declaration//GEN-END:variables
}
